﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Expect.Vs2008.Extensions.LanguageService
{
    public class ErrorHandler
    {
        const int errLev = 2;

        List<Error> errors;
        int errNum = 0;
        int wrnNum = 0;

        public bool Errors { get { return errNum > 0; } }
        public bool Warnings { get { return wrnNum > 0; } }
        public int ErrNum { get { return errNum; } }
        public int WrnNum { get { return wrnNum; } }

        public ErrorHandler()
        {
            errors = new List<Error>(8);
        }
        // -----------------------------------------------------
        //   Public utility methods
        // -----------------------------------------------------

        public List<Error> SortedErrorList()
        {
            if (errors.Count > 0)
            {
                errors.Sort();
                return errors;
            }
            else
            {
                return null;
            }
        }

        /*
        public void AddError(string msg, LexLocation span, int severity)
        {
            AddError(msg, span.sLin, span.sCol, span.eCol - span.sCol + 1, severity);
        }*/

        public void AddError(string msg, int lin, int col, int len, int severity)
        {
            bool warnOnly = severity < errLev;
            errors.Add(new Error(msg, lin, col, len, warnOnly));
            if (warnOnly) wrnNum++; else errNum++;
        }

        public void AddError(string msg, int lin, int col, int len)
        {
            errors.Add(new Error(msg, lin, col, len, false)); errNum++;
        }

        public void AddWarning(string msg, int lin, int col, int len)
        {
            errors.Add(new Error(msg, lin, col, len, true)); wrnNum++;
        }
    }
}
